<!-- 发票详情 -->
<template>
    <page-header-wrapper @back="() => $router.go(-1)">
        <a-card :bordered="false">
            <a-steps direction="horizontal" :current="invoice.state - 1" progressDot>
                <a-step title="申请开票">
                    <template v-slot:description>
                        <div class="antd-pro-pages-profile-advanced-style-stepDescription">
                            <div>{{invoice.billingTime}}</div>
                        </div>
                    </template>
                </a-step>
                <a-step title="审核通过" v-if="invoice.state != 9 && invoice.state != 8">
                    <template v-slot:description>
                        <div class="antd-pro-pages-profile-advanced-style-stepDescription">
                            <div>{{invoice.examineTime}}</div>
                        </div>
                    </template>
                </a-step>
                <a-step title="已开票" v-if="invoice.state != 9 && invoice.state != 8">
                    <template v-slot:description>
                        <div class="antd-pro-pages-profile-advanced-style-stepDescription">
                            <div>{{invoice.billingTime}}</div>
                        </div>
                    </template>
                </a-step>
                <a-step title="已邮寄" v-if="invoice.state != 9 && invoice.state != 8">
                    <template v-slot:description>
                        <div class="antd-pro-pages-profile-advanced-style-stepDescription">
                            <div>{{invoice.postTime}}</div>
                        </div>
                    </template>
                </a-step>
                <a-step title="已取消" v-if="invoice.state == 9">
                    <template v-slot:description >
                        <div class="antd-pro-pages-profile-advanced-style-stepDescription">
                            <div>{{invoice.updateTime}}</div>
                        </div>
                    </template>
                </a-step>
                <a-step title="已拒绝" v-if="invoice.state == 8">
                    <template v-slot:description >
                        <div class="antd-pro-pages-profile-advanced-style-stepDescription">
                            <div>{{invoice.updateTime}}</div>
                        </div>
                    </template>
                </a-step>
            </a-steps>

            <a-card style="margin-top: 24px" :bordered="false" title="发票信息">
                <a-descriptions size="small">
                    <a-descriptions-item label="发票抬头">{{invoice.receiptTitle}}</a-descriptions-item>
                    <a-descriptions-item label="基本开户银行" span="2">{{invoice.channel}}</a-descriptions-item>
                    <!-- <a-descriptions-item ></a-descriptions-item> -->
                    <a-descriptions-item label="发票类型">{{invoice.receiptType == 1 ?  '增值税普通发票' : '增值税专用发票'}}</a-descriptions-item>
                    <a-descriptions-item label="基本开户账号">{{invoice.bankAccount}}</a-descriptions-item>
                    <a-descriptions-item ></a-descriptions-item>
                    <a-descriptions-item label="纳税人识别号">{{!invoice.identificationNum || invoice.identificationNum == '' ? '-' : invoice.identificationNum}}</a-descriptions-item>
                    <a-descriptions-item label="企业注册地址">{{!invoice.companyRegisterAddress || invoice.companyRegisterAddress == '' ? '-' : invoice.companyRegisterAddress}}</a-descriptions-item>
                    <a-descriptions-item ></a-descriptions-item>
                    <a-descriptions-item label="税务登记固定电话">{{!invoice.taxRegisterTel || invoice.taxRegisterTel == '' ? '-' : invoice.taxRegisterTel}}</a-descriptions-item>
                    <a-descriptions-item label="备注" span="2">{{invoice.remark}}</a-descriptions-item>
                </a-descriptions>
                <a-divider/>
                <a-descriptions size="small">
                    <a-descriptions-item label="发票金额"><span style="color: orange">{{invoice.amount}}</span></a-descriptions-item>
                    <a-descriptions-item label="发票状态"><span style="color: orange">{{invoice.state | invoiceStateFilter}}</span></a-descriptions-item>
                    <a-descriptions-item ></a-descriptions-item>
                    <a-descriptions-item label="开票内容">{{invoice.receiptName}}</a-descriptions-item>
                    <a-descriptions-item label="邮寄地址" span="2">{{invoice.postAddress}}，{{invoice.postUserName}}，{{invoice.postPhone}}</a-descriptions-item>
                    <a-descriptions-item label="拒绝原因" span="3" v-if="invoice.state == 8">{{invoice.refuseReason}}</a-descriptions-item>
                    <a-descriptions-item label="发票查询码" v-if="invoice.state == 3 || invoice.state == 4">{{invoice.queryCode}} <a style="margin-left: 20px" href="javascript:void(0)" @click="updateQueryCode"><a-icon type="edit" /></a></a-descriptions-item>
                    <a-descriptions-item label="发票快照" span="2" v-if="invoice.state == 3 || invoice.state == 4">{{invoice.receiptUrl}} <a style="margin-left: 20px" href="javascript:void(0)" @click="openUrl">查看</a> <a style="margin-left: 20px" href="javascript:void(0)" @click="updateReceiptUrl"><a-icon type="edit" /></a></a-descriptions-item>
                    <a-descriptions-item label="邮寄信息" span="3" v-if="invoice.state == 4">{{invoice.postCompany}}，{{invoice.postNumber}} <a style="margin-left: 20px" href="javascript:void(0)" @click="mail(1)"><a-icon type="edit" /></a></a-descriptions-item>
                </a-descriptions>
            </a-card>

            <a-card style="margin-top: 24px" :bordered="false" title="关联结算信息">
                <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" size="small">
                    <div slot="operation" slot-scope="text,record">
                        <div>{{(parseFloat(record.successAmount) + parseFloat(record.successServiceAmount)).toFixed(2)}}</div>
                    </div>
                </a-table>
            </a-card>

            <div style="margin-top: 20px" class="flex flexCenter" v-if="invoice.state == 1 && hasPower('refuseReceipt')">
                <a-button type='primary' style="margin-right: 20px" size="large" @click="success"><span style="padding: 0 50px">通过</span></a-button>
                <a-button type='danger' size="large" @click="refuse"><span style="padding: 0 50px">拒绝</span></a-button>
            </div>
            <div style="margin-top: 20px" class="flex flexCenter" v-if="invoice.state == 2 && hasPower('makeReceipt')">
                <a-button  type='primary' size="large" @click="makes"><span style="padding: 0 50px">开票</span></a-button>
            </div>
            <div style="margin-top: 20px" class="flex flexCenter" v-if="invoice.state == 3">
                <a-button  type='primary' size="large" @click="mail(2)"><span style="padding: 0 50px">邮寄</span></a-button>
            </div>
        </a-card>

        <!-- 拒绝原因 -->
        <a-modal
            title="添加拒绝原因"
            :visible="refuseVisible"
            @ok="refuseHandleOk"
            @cancel="refuseVisible = false"
            >
            <div>
                <a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 17 }">
                    <a-form-item label="拒绝原因" help="拒绝开票，请向客户说明拒绝原因">
                        <a-textarea :rows="4" v-model="refuseReason" allow-clear placeholder="请输入拒绝原因" :maxLength="100"/>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>

        <!-- 开票设置 -->
        <a-modal
            title="开票设置"
            :visible="makeVisible"
            @ok="makeHandleOk"
            @cancel="makeVisible = false"
            >
            <div>
                <a-form-model :model="makeConfig" ref="ruleForm" :rules="rules" :label-col="{ span: 6 }" :wrapper-col="{ span: 17 }">
                    <a-form-item label="发票查询码">
                        <a-input v-model="makeConfig.queryCode" allow-clear placeholder="请输入发票查询码" />
                    </a-form-item>
                    <a-form-item label="发票快照" prop="receiptUrl">
                        <a-upload
                            name="file"
                            @change="handleChangeInvoice"
                            :before-upload="beforeUpload"
                            :fileList = invoiceFileList
                            accept=".pdf,.jpg,.png,.jpeg"
                            :remove="()=>{invoiceFileList = []}"
                        >
                            <a-button type="primary"> 上传 </a-button>
                            <p class="ant-upload-text" style="margin-top: 10px">
                                仅支持上传PDF、jpg、png、jpeg格式，文件大小最多10M
                            </p>
                        </a-upload>
                    </a-form-item>
                </a-form-model>
            </div>
        </a-modal>

        <!-- 邮递信息 -->
        <a-modal
            title="邮寄设置"
            :visible="mailVisible"
            @ok="mailHandleOk"
            @cancel="mailVisible = false"
            >
            <div>
                <a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 17 }">
                    <a-form-item label="快递公司" >
                        <a-input v-model="postCompany" allow-clear placeholder="请输入快递公司" />
                    </a-form-item>
                    <a-form-item label="快递单号" help="快递公司和快递单号用于客户查询邮寄信息；">
                        <a-input v-model="postNumber" allow-clear placeholder="请输入快递单号" />
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>

        <!-- 修改发票查询码 -->
        <a-modal
            title="开票设置"
            :visible="queryCodeVisible"
            @ok="queryCodeHandleOk"
            @cancel="queryCodeVisible = false"
            >
            <div>
                <a-form-model :label-col="{ span: 6 }" :wrapper-col="{ span: 17 }">
                    <a-form-item label="发票查询码">
                        <a-input v-model="queryCode" allow-clear placeholder="请输入发票查询码" />
                    </a-form-item>
                </a-form-model>
            </div>
        </a-modal>
        <!-- 修改发票快照 -->
        <a-modal
            title="开票设置"
            :visible="receiptUrlVisible"
            @ok="receiptUrlHandleOk"
            @cancel="receiptUrlVisible = false"
            >
            <div>
                <a-form-model :model="makeConfig" ref="ruleForm" :rules="rules" :label-col="{ span: 6 }" :wrapper-col="{ span: 17 }">
                    <a-form-item label="发票快照" prop="receiptUrl">
                        <a-upload
                            name="file"
                            @change="handleChangeInvoice"
                            :before-upload="beforeUpload"
                            :fileList = invoiceFileList
                            accept=".pdf,.jpg,.png,.jpeg"
                            :remove="()=>{invoiceFileList = []}"
                        >
                            <a-button type="primary"> 上传 </a-button>
                            <p class="ant-upload-text" style="margin-top: 10px">
                                仅支持上传PDF、jpg、png、jpeg格式，文件大小最多10M
                            </p>
                        </a-upload>
                    </a-form-item>
                </a-form-model>
            </div>
        </a-modal>
    </page-header-wrapper>
</template>

<script>
import { columns } from './table';
import { getReceiptDetail , getReceiptRelationSettlementList , updateReceipt , adoptReceipt , refuseReceipt , makeReceipt , mailReceipt } from '@/api/services/invoice';
import { fileUpload } from '@/api/system';
import { cancelReceipt } from '@/api/companys/invoice';
export default {
    data () {
        return {
            isload: false,
            columns,
            dataList: [],
            invoice: {
                id: '',
                state: 1,
            },
            receiptId: '',
            refuseVisible: false,
            refuseReason: '',
            makeVisible: false,
            invoiceFileList: [],
            makeConfig: {
                queryCode: '', // 查询码
                receiptUrl: '',
                receiptId: '', // 发票ID
            }, // 开票内容
            rules: {
                contractUrl: [{ required: true, message: '请上传发票快照', trigger: 'blur' }],
            },
            mailVisible: false, 
            mailType: 1,
            postCompany: '', // 快递公司
            postNumber: '', // 快递单号
            queryCode: '', // 发票查询码
            queryCodeVisible: false,
            receiptUrlVisible: false,
        };
    },
    created(){
        this.receiptId = this.$route.query.receiptId;
        this.getData();
        this.getDetail();
    },
    methods: {
        // 获取账户列表数据
        getData(){
            let _this = this;
            _this.isload = true;
            getReceiptRelationSettlementList(_this.receiptId)
            .then(res => {
                _this.isload = false;
                if(res.success){
                    _this.dataList = res.data;
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        // 获取发票详情信息
        getDetail(){
            let _this = this;
            getReceiptDetail(_this.receiptId)
            .then(res => {
                if(res.success){
                    _this.invoice = res.data;
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        // 撤销发票申请
        cancels(){
            let _this = this;
            _this.$confirm({
                title: '温馨提示',
                content: '撤销后，供应商将不再处理该发票申请，请谨慎操作；',
                onOk() {
                    cancelReceipt(_this.receiptId)
                    .then(res => {
                        if(res.success){
                            _this.$notification['success']({
                                message: '温馨提示',
                                description: '操作成功',
                                duration: 8
                            })
                            _this.$router.go(-1);
                        }else{
                            _this.$notification['error']({
                                message: '温馨提示',
                                description: res.errMessage,
                                duration: 8
                            })
                        }
                    })
                },
                onCancel() {},
            });
        },
        // 查看发票快照
        openUrl(){
            window.open(this.invoice.receiptUrl);
        },
        // 修改发票查询码
        updateQueryCode(){
            this.queryCode = this.invoice.queryCode;
            this.queryCodeVisible = true;
        },
        queryCodeHandleOk(){
            let _this = this;
            if(_this.queryCode == ''){
                _this.$message.warning('请输入发票查询码');
                return;
            }
            const params = {
                'receiptId': _this.receiptId,
                'updateType': 1,
                'queryCode': _this.queryCode,
            }
            _this.updateInvoice(params);
        },
        // 修改发票快照
        updateReceiptUrl(){
            this.receiptUrl = '';
            this.receiptUrlVisible = true;
        },
        receiptUrlHandleOk(){
            let _this = this;
            if(_this.receiptUrl == ''){
                _this.$message.warning('请输入发票查询码');
                return;
            }
            const params = {
                'receiptId': _this.receiptId,
                'updateType': 2,
                'receiptUrl': _this.receiptUrl,
            }
            _this.updateInvoice(params);
        },
        // 修改发票查询码、快照、快递信息
        updateInvoice(params){
            let _this = this;
            updateReceipt(params)
            .then(res => {
                if(res.success){
                    _this.$notification['success']({
                        message: '温馨提示',
                        description: '操作成功',
                        duration: 8
                    })
                    _this.queryCodeVisible = false;
                    _this.receiptUrlVisible = false;
                    _this.mailVisible = false;
                    _this.getDetail();
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        // 通过审核
        success(){
            let _this = this
            _this.$confirm({
                title: '温馨提示',
                content: '审核通过后，将不可取消通过状态，请谨慎操作；',
                onOk() {
                    adoptReceipt(_this.receiptId)
                    .then(res => {
                        if(res.success){
                            _this.$notification['success']({
                                message: '温馨提示',
                                description: '操作成功',
                                duration: 8
                            })
                            _this.getDetail();
                        }else{
                            _this.$notification['error']({
                                message: '温馨提示',
                                description: res.errMessage,
                                duration: 8
                            })
                        }
                    })
                },
                onCancel() {},
            });
        },
        // 拒绝审核
        refuse(){
            this.refuseVisible = true;
        },
        refuseHandleOk(){
            let _this = this
            const params = {
                'receiptId': _this.receiptId,
                'refuseReason': _this.refuseReason,
            }
            refuseReceipt(params)
            .then(res => {
                if(res.success){
                    _this.$notification['success']({
                        message: '温馨提示',
                        description: '操作成功',
                        duration: 8
                    })
                    _this.refuseVisible = false;
                    _this.getDetail();
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        // 开票
        makes(){
            this.makeConfig.receiptId = this.receiptId;
            this.makeConfig.queryCode = ''
            this.makeConfig.receiptUrl = ''
            this.makeVisible = true;
        },
        beforeUpload(file) {
            return false;
        },
        handleChangeInvoice({file,fileList}){
            let _this = this
            const status = file.status;
            if(file.status === 'removed'){
                this.invoiceFileList = []
                return
            }
            this.invoiceFileList = fileList
            const formData = new FormData();
            formData.append('multipartFile',file)
            fileUpload(formData)
            .then(res=>{
                if(res.success){
                    _this.makeConfig.receiptUrl = res.data
                    _this.receiptUrl = res.data
                    _this.$refs.ruleForm.clearValidate()
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        makeHandleOk(){
            let _this = this
            _this.$refs.ruleForm.validate(valid => {
                if(valid){
                    makeReceipt(_this.makeConfig).then(res=>{
                        if(res.success){
                            _this.makeVisible = false;
                            _this.getDetail();
                            _this.$notification['success']({
                                message: '温馨提示',
                                description: '开票成功',
                                duration: 8
                            })
                        }else{
                            _this.$notification['error']({
                                message: '温馨提示',
                                description: res.errMessage,
                                duration: 8
                            })
                        }
                    })
                }
            })
        },
        // 邮寄
        mail(type){
            this.mailType = type;
            this.mailVisible = true;
        },
        mailHandleOk(){
            let _this = this
            if(_this.postCompany == ''){
                _this.$message.warning('请输入快递公司名称');
                return;
            }
            if(_this.postNumber == ''){
                _this.$message.warning('请输入快递单号');
                return;
            }
            if(_this.mailType == 2){
                const params = {
                    'receiptId': _this.receiptId,
                    'postCompany': _this.postCompany,
                    'postNumber': _this.postNumber,
                }
                mailReceipt(params)
                .then(res => {
                    if(res.success){
                        _this.$notification['success']({
                            message: '温馨提示',
                            description: '邮寄成功',
                            duration: 8
                        })
                        _this.mailVisible = false;
                        _this.getDetail();
                    }else{
                        _this.$notification['error']({
                            message: '温馨提示',
                            description: res.errMessage,
                            duration: 8
                        })
                    }
                })
            }else{
                const params = {
                    'receiptId': _this.receiptId,
                    'postCompany': _this.postCompany,
                    'updateType': 3,
                    'postNumber': _this.postNumber,
                }
                _this.updateInvoice(params);
            }
        },
    }
}

</script>
<style scoped>

</style>